package com.gxa.ehome.controller;

import com.alibaba.fastjson.JSON;
import com.gxa.ehome.pojo.User;
import com.gxa.ehome.util.MD5Util;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Controller
//localhost:8080/ehome/user
@RequestMapping("/user")
public class UserController  {

    //localhost:8080/ehome/user/index
    //localhost:8080/ehome/user/index.do
    //localhost:8080/ehome/user/index.action  stuct2的风格

    @RequestMapping("/index")
    public String index(ModelMap modelMap){
        System.out.println("有用户请求index");

        //异常测试
//        System.out.println(12/0);

//        可以通过modelMap给前端传递数据
        modelMap.addAttribute("username","张三丰");

        //该返回值表示jsp名称，前后缀已经配置好了
        //会以转发的方式访问该jsp页面
        return "index";
    }

    @RequestMapping(value = "/login/username/{username}/password/{password}",method = RequestMethod.POST)
    public  String login(@PathVariable("username")String username,@PathVariable("password")String password){
        System.out.println(username);
        System.out.println(password);

        return "";
    }

    @RequestMapping(value = "/register/id/{id}" , method = RequestMethod.POST)
    public String register(@PathVariable("id") Integer id){
        System.out.println("有请求进来,请求的id是:"+id);

        return "";
    }

    @RequestMapping(value = "/register2" , method = RequestMethod.POST)
    public String register2(@RequestParam(name = "data")String dataString ){
       System.out.println(dataString);
        User user = JSON.parseObject(dataString, User.class);
        System.out.println(user);
        return "";
    }

/*    @RequestMapping(value = "/login2",method = RequestMethod.POST)
    @ResponseBody
    public  String login2(@RequestBody User user){
        System.out.println(user);

        return "{\"sta\":true}";
    }*/

    @RequestMapping(value = "/login2",method = RequestMethod.POST)
    @ResponseBody
    public  User login2(@RequestBody User user){
        System.out.println(user);

        user.setUsername(user.getUsername()+"2");
        user.setPassword(MD5Util.encrypt(user.getPassword()));

        return user;
    }

    @RequestMapping(value = "/select")
    @ResponseBody
    public List<User> select(){
        User user = new User();
        user.setUsername("张三");

        User user2 = new User();
        user2.setUsername("张三");

        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user2);

        return users;


    }

    @RequestMapping(value = "/select2")
    @ResponseBody
    public Map<String,Object> select2(){
        User user = new User();
        user.setUsername("张三1");

        User user2 = new User();
        user2.setUsername("张三1");

        List<User> users = new ArrayList<>();
        users.add(user);
        users.add(user2);

        Map<String,Object> jsonMap = new Hashtable<>();
        jsonMap.put("data",users);
        jsonMap.put("code",0);
        jsonMap.put("msg","用户data");
        jsonMap.put("count",100);
        return jsonMap;

    }

    //使用jsp的EL表达式
    @ModelAttribute("myUser")
    public User getUser(){
        User user = new User();
        user.setUsername("张大炮");
        user.setPassword(MD5Util.encrypt("qqq67654456"));

        return user;

    }


}
